﻿using Business;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApi.Controllers
{
    public class LocationGoodsController : ApiController
    {
        private static LocationGoodsBll _NewInstance = new LocationGoodsBll();

        public static LocationGoodsBll NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new LocationGoodsBll();
                }

                return _NewInstance;
            }
        }

        /// <summary>
        /// 拣货仓货位商品关关联
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="LocationCode"></param>
        /// <param name="GoodsCode"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel SetPickLocationGoods(string userID, string LocationCode, string GoodsCode)
        {
            if (string.IsNullOrEmpty(userID) || string.IsNullOrEmpty(LocationCode) || string.IsNullOrEmpty(GoodsCode))
                return new ResultModel() { RESULT = "failed", MESSAGE = "参数有空值", DATA = null };

            int LOCATIONID = NewInstance.GetLocationID(LocationCode); //获取货位ID
            if (LOCATIONID <= 0)
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "货位" + LocationCode + "不存在！", DATA = null };
            }
            string WareHouseCode = NewInstance.GetWarehouseCodeByLoctionID(LOCATIONID);//获取货位所属仓位编码
            if (WareHouseCode != "06")
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "货位" + LocationCode + "不属于分拣仓，请重新扫描/输入货位号！", DATA = null };
            }
            int GOODSID = NewInstance.GetGoodsID(GoodsCode); //获取上架商品ID
            if (GOODSID <= 0)
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "商品" + GoodsCode + "不存在！", DATA = null };
            }


            string isLocationCode = NewInstance.IsExistGoods(GOODSID, WareHouseCode, 0);//判断该商品在当前仓位是否有有效关联货位
            if (!string.IsNullOrEmpty(isLocationCode))
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "商品" + GoodsCode + "已存在关联货位" + isLocationCode + "！", DATA = null };
            }

            if (GoodsCode.Length > 0 && GoodsCode.Substring(0, 1) != "W" && WareHouseCode == "06")
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "非WOC商品不能关联拣货仓货位" + LocationCode + "！", DATA = null };
            }

            string StrSuccess = "";
            string isGoodsCode = NewInstance.IsExistGoods2(LOCATIONID, WareHouseCode, 0);
            if (!string.IsNullOrEmpty(isGoodsCode))
            {
                int isGOODSID = NewInstance.GetGoodsID(isGoodsCode); //获取已存在关联商品ID
                if (NewInstance.SetLocationGoods(isGOODSID, LOCATIONID, userID, 1) == 0)
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "原货位" + LocationCode + "关联商品" + isGoodsCode + "作废操作失败，无法关联新的商品！", DATA = null };
                }
                Goods GoodsInfo = NewInstance.GetGoodsByGoodID(isGOODSID);
                if (GoodsInfo != null)
                    StrSuccess += "货位" + LocationCode + "已解除与商品" + isGoodsCode + "-->" + GoodsInfo.GOODSNAME + "的关联关系，并关联商品" + LocationCode;
            }

            if (NewInstance.SetLocationGoods(GOODSID, LOCATIONID, userID, 0) != 0)
                return new ResultModel() { RESULT = "success", MESSAGE = StrSuccess, DATA = null };
            else
                return new ResultModel() { RESULT = "failed", MESSAGE = "关联失败", DATA = null };
        }

        /// <summary>
        /// 检查拣货仓货位商品关关联
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="LocationCode"></param>
        /// <param name="GoodsCode"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel CheckPickLocationGoods(string userID, string LocationCode, string GoodsCode)
        {
            try
            {
                if (string.IsNullOrEmpty(userID) || string.IsNullOrEmpty(LocationCode) || string.IsNullOrEmpty(GoodsCode))
                    return new ResultModel() { RESULT = "failed", MESSAGE = "参数有空值", DATA = null };

                int LOCATIONID = NewInstance.GetLocationID(LocationCode); //获取货位ID
                if (LOCATIONID <= 0)
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "货位" + LocationCode + "不存在！", DATA = null };
                }
                string WareHouseCode = NewInstance.GetWarehouseCodeByLoctionID(LOCATIONID);//获取货位所属仓位编码
                if (WareHouseCode != "06")
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "货位" + LocationCode + "不属于分拣仓，请重新扫描/输入货位号！", DATA = null };
                }
                int GOODSID = NewInstance.GetGoodsID(GoodsCode); //获取上架商品ID
                if (GOODSID <= 0)
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "商品" + GoodsCode + "不存在！", DATA = null };
                }


                string isLocationCode = NewInstance.IsExistGoods(GOODSID, WareHouseCode, 0);//判断该商品在当前仓位是否有有效关联货位
                if (!string.IsNullOrEmpty(isLocationCode))
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "商品" + GoodsCode + "已存在关联货位" + isLocationCode + "！", DATA = null };
                }

                if (GoodsCode.Length > 0 && GoodsCode.Substring(0, 1) != "W" && WareHouseCode == "06")
                {
                    return new ResultModel() { RESULT = "failed", MESSAGE = "非WOC商品不能关联拣货仓货位" + LocationCode + "！", DATA = null };
                }

                string isGoodsCode = NewInstance.IsExistGoods2(LOCATIONID, WareHouseCode, 0);
                if (!string.IsNullOrEmpty(isGoodsCode))
                {
                    return new ResultModel() { RESULT = "Spesuccess", MESSAGE = "货位" + LocationCode + "处于拣货仓，且已存在关联商品" + isGoodsCode + "！", DATA = null };
                }
                return new ResultModel() { RESULT = "success", MESSAGE = "货位" + LocationCode + "与关联商品" + GoodsCode + "检查无误！", DATA = null };
            }
            catch (Exception ex)
            {
                return new ResultModel() { RESULT = "failed", MESSAGE = "验证出错，详细信息：" + ex.Message, DATA = null };
            }

        }
    }
}